package br.com.beautycenter.view;

import br.com.beautycenter.Funcionalidades.Bean;
import br.com.beautycenter.Funcionalidades.Swing;
import br.com.beautycenter.Funcionalidades.Validacoes;
import br.com.beautycenter.DAO.PersisteCargo;
import br.com.beautycenter.bean.Cargo;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

public class CadastroComissao extends javax.swing.JFrame {

    PersisteCargo persisteCargo = new PersisteCargo();
    Cargo cargo = new Cargo();
    Cargo cargoTemp = new Cargo();
    static List<Cargo> listaCargo = new ArrayList<Cargo>(); //lista com todos os funcionários

    public CadastroComissao() {
        initComponents();
        iniciarTela();
    }
    /**
     * operacao = 0 -> salvar operacao = 1 -> atualizar operacao = 2 -> excluir
     */
    int operacao = 0;
    int posicao = -1;

    private void iniciarTela() {
        limparCampos();
        desativarCampos();
        listaCargo = persisteCargo.retornarCargos();
        ativarBotoesIniciar();
    }

    public void ativarBotoesNovoEditarExcluir() {
        Swing.desativarCampos(jPanelBotoes.getComponents());
        btnSalvar.setEnabled(true);
        btnCancelar.setEnabled(true);
        btnSair.setEnabled(true);
    }

    /**
     * Inicialmente os botões ativos serão: <br/>
     * btnNovo<br/>
     * btnEditar<br/>
     * btnPesquisar<br/>
     * btnSair<br/>
     */
    private void limparCampos() {
        Swing.zerarCampos(jPanelPrincipal.getComponents());
    }

    private void desativarCampos() {
        Swing.desativarCampos(jPanelPrincipal.getComponents());
    }

    private void ativarBotoesIniciar() {
        Swing.desativarCampos(jPanelBotoes.getComponents());

        //se a lista estiver vazia, cria um novo
        if (listaCargo.isEmpty()) {
            novo();
        } else if (posicao < 0) {
            btnNovo.setEnabled(true);
            btnPesquisar.setEnabled(true);
            btnSair.setEnabled(true);
        } else {
            btnNovo.setEnabled(true);
            btnPesquisar.setEnabled(true);
            btnSair.setEnabled(true);
            btnEditar.setEnabled(true);
            btnExcluir.setEnabled(true);
        }
    }

    private void ativarCampos() {
        Swing.ativarCampos(jPanelPrincipal.getComponents());

        txtComissao.setEnabled(true);

    }

    public void ativarBotoesSalvarCancelarSair() {
        Swing.desativarCampos(jPanelBotoes.getComponents());
        btnSalvar.setEnabled(true);
        btnCancelar.setEnabled(true);
        btnSair.setEnabled(true);
    }

    public boolean validarDescricao() {
        if (!Validacoes.validarCampoTexto(txtComissao.getText().trim())) {
            JOptionPane.showMessageDialog(null, "Campo Cargo é obrigatório!");
            txtComissao.requestFocus();
            return false;
        }

        cargoTemp.setNomeCargo(txtComissao.getText().trim());
        return true;
    }

    public void novo() {
        operacao = 0;
        limparCampos();
        ativarCampos();
        ativarBotoesSalvarCancelarSair();

        txtComissao.requestFocus();
    }

    public boolean preencherCargo() {

        cargoTemp = new Cargo();
        if (validarDescricao()) {

            cargoTemp.setNomeCargo(txtComissao.getText());

            //já que vai salvar, copia os dados para os originais
            Bean.copiarCargo(cargoTemp, cargo);

            //atualiza ou salva o funcionário
            if (operacao == 1) {
                persisteCargo.atualizar(cargo);
            } else {
                persisteCargo.salvar(cargo);
                posicao++;
            }
            JOptionPane.showMessageDialog(null, "Cargo salvo com sucesso");
            operacao = -1;
            return true;
        }

        return false;
    }

    public void editar() {
        operacao = 1;

        exibirCargos();
        ativarCampos();
        ativarBotoesNovoEditarExcluir();


        cargo = new Cargo();
        cargo = listaCargo.get(posicao);

        txtComissao.requestFocus();
    }

    public void exibirCargos() {

        if (listaCargo.isEmpty()) {
            novo();
            return;
        }

        limparCampos();

        txtComissao.setText(listaCargo.get(posicao).getNomeCargo());

        desativarCampos();
    }

    public void excluir() {
        int resposta = JOptionPane.showConfirmDialog(null, "Deseja realmente excluir esse cargo?");

        if (resposta == JOptionPane.OK_OPTION) {
            //  listaCargo.get(posicao).setAtivo(false);
            persisteCargo.atualizar(listaCargo.get(posicao));
            exibirCargos();
        }

        operacao = -1;
        ativarBotoesNovoEditarExcluir();
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelBotoes = new javax.swing.JPanel();
        btnNovo = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnPesquisar = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();
        jPanelPrincipal = new javax.swing.JPanel();
        jLabelCargo = new javax.swing.JLabel();
        txtComissao = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ERP BEAUTY CENTER - Cadastro Cargo");

        jPanelBotoes.setPreferredSize(new java.awt.Dimension(800, 45));

        btnNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Novo.png"))); // NOI18N
        btnNovo.setToolTipText("NOVO");
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Editar.png"))); // NOI18N
        btnEditar.setToolTipText("ALTERAR");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        btnExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Excluir.png"))); // NOI18N
        btnExcluir.setToolTipText("EXCLUIR");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        btnPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Pesquisar.png"))); // NOI18N
        btnPesquisar.setToolTipText("PESQUISAR");
        btnPesquisar.setBorderPainted(false);
        btnPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarActionPerformed(evt);
            }
        });

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Salvar.png"))); // NOI18N
        btnSalvar.setToolTipText("SALVAR");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Cancelar.png"))); // NOI18N
        btnCancelar.setToolTipText("LIMPAR");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Sair.png"))); // NOI18N
        btnSair.setToolTipText("SAIR");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelBotoesLayout = new javax.swing.GroupLayout(jPanelBotoes);
        jPanelBotoes.setLayout(jPanelBotoesLayout);
        jPanelBotoesLayout.setHorizontalGroup(
            jPanelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBotoesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnNovo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEditar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnExcluir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnPesquisar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSalvar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancelar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSair)
                .addContainerGap(19, Short.MAX_VALUE))
        );
        jPanelBotoesLayout.setVerticalGroup(
            jPanelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBotoesLayout.createSequentialGroup()
                .addGroup(jPanelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnNovo)
                    .addComponent(btnEditar)
                    .addComponent(btnExcluir)
                    .addComponent(btnPesquisar)
                    .addComponent(btnSalvar)
                    .addComponent(btnCancelar)
                    .addComponent(btnSair))
                .addContainerGap())
        );

        jPanelPrincipal.setBackground(new java.awt.Color(255, 255, 255));
        jPanelPrincipal.setPreferredSize(new java.awt.Dimension(800, 145));

        jLabelCargo.setText("Comissao:");

        javax.swing.GroupLayout jPanelPrincipalLayout = new javax.swing.GroupLayout(jPanelPrincipal);
        jPanelPrincipal.setLayout(jPanelPrincipalLayout);
        jPanelPrincipalLayout.setHorizontalGroup(
            jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPrincipalLayout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addComponent(jLabelCargo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtComissao, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(202, Short.MAX_VALUE))
        );
        jPanelPrincipalLayout.setVerticalGroup(
            jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPrincipalLayout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addGroup(jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCargo)
                    .addComponent(txtComissao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(60, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelBotoes, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
            .addComponent(jPanelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanelBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
        novo();
    }//GEN-LAST:event_btnNovoActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        editar();
    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        excluir();
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void btnPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarActionPerformed
        PesquisarCargo pesquisarCargo = new PesquisarCargo(this, true);
        pesquisarCargo.setLocationRelativeTo(null);
        pesquisarCargo.setVisible(true);

        ativarBotoesIniciar();

    }//GEN-LAST:event_btnPesquisarActionPerformed
    public void cancelar() {
        limparCampos();
        desativarCampos();
    }
    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        if (preencherCargo()) {
            iniciarTela();
        }
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        cancelar();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        dispose();
    }//GEN-LAST:event_btnSairActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnPesquisar;
    private javax.swing.JButton btnSair;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JLabel jLabelCargo;
    private javax.swing.JPanel jPanelBotoes;
    private javax.swing.JPanel jPanelPrincipal;
    private javax.swing.JTextField txtComissao;
    // End of variables declaration//GEN-END:variables
}